package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import att.grappa.Node;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/3/11
 * Time: 8:36 AM
 * To change this template use File | Settings | File Templates.
 */
public class DeleteTaskListener implements ActionListener
{


    String jobTag;
    Connection connection;
    GraphVizGraph graphVizGraph;
    EditIntFrame editIntFrame;


    public DeleteTaskListener(String jobName, GraphVizGraph graphVizGraph, Connection connection, EditIntFrame e)
    {
        this.jobTag = jobName;
        this.connection = connection;
        this.graphVizGraph = graphVizGraph;
        this.editIntFrame = e;
    }


    public void actionPerformed(ActionEvent e)
    {
        Node[] nodes = graphVizGraph.getGraph().nodeElementsAsArray();
        String[] currentNodeNames = new String[nodes.length];
        for (int i = 0; i < nodes.length; i++)
        {
            currentNodeNames[i] = nodes[i].getName();
        }

        Arrays.sort(currentNodeNames);
        JComboBox cbox1 = new JComboBox(currentNodeNames);
        Object[] message = new Object[]{"Task_Tag", cbox1};
        int input = JOptionPane.showConfirmDialog(null, message, "Select the task to delete", JOptionPane.OK_CANCEL_OPTION);
        if (input == JOptionPane.OK_OPTION)
        {
            String taskName = cbox1.getSelectedItem().toString();

            CallStoredProc storedProc = new CallStoredProc("{call tms.task_util.remove(?,?)}", new String[]{jobTag, taskName}, connection);

            editIntFrame.sqlCommands.add(storedProc);
            editIntFrame.model.addElement(storedProc.toString());
            editIntFrame.sqlcommandsList.setModel(editIntFrame.model);
        }
    }


}


